package com.lss.controller;


import com.lss.bean.Student;
import com.lss.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WebController {

    /**
     * 容器自动注入
     */
    @Autowired
    StudentService service;

    /**
     * 登录页面的请求
     */
    @RequestMapping({"/login", "/"})
    public String login(Model model, HttpSession session) {
        String msg = (String) session.getAttribute("msg");
        model.addAttribute("msg", msg);
        return "Login";
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("/student")
    public String student(String username, String password) {
        return "Student";
    }

    /**
     * 修改密码
     */
    @RequestMapping("/changePassword")
    public String Revise() {
        return "changePassword";
    }

    /**
     * 学生信息展示
     */
    @RequestMapping("/list")
    public String List(Model model) {
        List<Student> students = service.selectAll();
        model.addAttribute("students", students);
        return "list";
    }

    /**
     * 删除学生
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam("id") String id,
                         HttpServletRequest request) {
        service.deleteStudent(id);
        return "redirect:/limit?start=0&rows=6";
    }

    /**
     * 进入修改学生信息的页面
     */
    @RequestMapping("/update")
    public String update(@RequestParam("id") String id, Model model) {
        Student student = service.selectById(id);
        model.addAttribute("student", student);
        return "update";
    }

    /**
     * 修改学生信息，并且返回首页
     */
    @RequestMapping("/updateEnd")
    public String updateEnd(Student student) {
        service.updateStudent(student);
        return "redirect:/limit?start=0&rows=6";
    }

    /**
     * 分页查询数据
     */
    @RequestMapping("/limit")
    public String limitFindStudent(@RequestParam("start") int start,
                                   @RequestParam("rows") int rows,
                                   Model model) {
        List<Student> students = service.limitFindStudent(start, rows);
        model.addAttribute("students", students);
        List<Student> selectAll = service.selectAll();
        model.addAttribute("count", selectAll);
        model.addAttribute("start", start);
        return "list";
    }

    @RequestMapping("/goToAddStudentPage")
    public String goToAddStudentPage() {
        return "addStudent";
    }

    /**
     * 添加学生
     */
    @RequestMapping("/addStudent")
    public String addStudent(Student student) {
        Student selectById = service.selectById(student.getId());
        if (selectById != null) {           //如果学号重复，返回页面
            return "redirect:/goToAddStudentPage";
        }
        service.addStudent(student);
        return "redirect:/limit?start=0&rows=6";
    }

    /**
     * 检查学号是否重复,然后进行提示
     */
    @ResponseBody
    @RequestMapping("/checking")
    public int checking(String id) {
        Student student = service.selectBeingStudent(id);
        if (student == null) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 查询学生信息
     */
    @RequestMapping("/findStudentNews")
    public String findStudentNews(String id, String name, String address, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!id.equals("")) {
            map.put("id", id);
        }
        if (!name.equals("")) {
            map.put("name", name);
        }
        if (!address.equals("")) {
            map.put("address", address);
        }
        map.put("start", 0);
        map.put("rows", 6);
        List<Student> studentNewsByLimit = service.findStudentNewsByLimit(map);
        model.addAttribute("students", studentNewsByLimit);
        List<Student> studentNews = service.findStudentNews(map);
        model.addAttribute("count", studentNews);
        model.addAttribute("start", 0);
        return "list";
    }

    /**
     * 返回到登录页面
     */
    @RequestMapping("/backLogin")
    public String backLogin(HttpSession httpSession) {
        httpSession.removeAttribute("login");
        return "redirect:/redirect:/limit?start=0&rows=6";
    }

    @RequestMapping("/Top")
    public String Top() {
        return "Top";
    }

    @RequestMapping("/Left")
    public String Left() {
        return "Left";
    }
}
